package com.roy.royminidao.common.framework.lang.web.springmvc;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/***
 * 
 * spring上下文获取工具类
 * 
 * <pre>
 * 主要提供spring 中上下文内容的获取，文件的读取，bean的读取
 * </pre>
 * 
 * @author roy
 * @see
 * @since 1.0.0
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {

    private static Logger logger = LoggerFactory.getLogger(SpringContextUtil.class);

    private static ApplicationContext context;

    public static ApplicationContext getApplicationContext() {
        return context;
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;

    }

    public static Object getBean(String beanName) {
        logger.debug("查找bean:{}", beanName);
        return context.getBean(beanName);
    }

    /**
     * 根据Bean名称返回String类型的BEAN
     * 
     * @param beanName
     * @return
     */
    public static String getStringBean(String beanName) {
        logger.info("查找bean:{}", beanName);
        return context.getBean(beanName, String.class);
    }

}
